<template>
  <div>
    <van-sticky>
      <van-nav-bar title="黑马头条 - 登录" />
    </van-sticky>

    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[
          {
            required: true,
            message: '请输入正确的手机号',
            pattern: /^1[3456789]\d{9}$/,
          },
        ]"
      />
      <van-field
        v-model="user.code"
        type="text"
        name="code"
        label="验证码"
        placeholder="请输入密码"
        required
        :rules="[
          { required: true, message: '请输入万能验证码', pattern: /^\d{6}$/ },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          block
          type="info"
          native-type="submit"
          :loading="isLoading"
          :disabled="isLoading"
          loading-text="登陆中..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      user: {
        mobile: '13888888888',
        code: '246810'
      },
      isLoading: false // 控制登录按钮加载状态
    }
  },
  methods: {
    ...mapMutations(['changeRefreshToken', 'changetoken']),
    async  onSubmit (value) {
      this.isLoading = true
      const [err, res] = await loginAPI(value)
      this.isLoading = false
      if (err) this.$Notify({ type: 'danger', message: '账号密码登录' })
      else this.$Notify({ type: 'success', message: '登录成功' })
      const { refreshToken, token } = res.data.data
      this.changeRefreshToken(refreshToken)
      this.changetoken(token)
      console.log(res)
    }
  }
}
</script>

<style scoped lang="less">
</style>
